package cn.thinkjoy.LawDataForExcel.service.Impl;

import cn.thinkjoy.LawDataForExcel.bean.CompanyInfo;
import cn.thinkjoy.LawDataForExcel.dao.CompanyInfoDao;
import cn.thinkjoy.LawDataForExcel.service.interfaces.CompanyInfoService;
import cn.thinkjoy.LawDataForExcel.service.redis.SingleCommonCacheService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by thinkjoy on 2017/6/20.
 */
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoDao,CompanyInfo> implements CompanyInfoService{
    @Autowired
    private CompanyInfoDao companyInfoDao;
    @Autowired
    private SingleCommonCacheService<CompanyInfo> redisCompanyInfo;

    public int saveCompanyInfo(CompanyInfo companyInfo) {
        return companyInfoDao.saveCompanyInfo(companyInfo);
    }

    @Override
    public CompanyInfo findCompanyInfoByName(String cname) {
        CompanyInfo companyInfo;
        try {
            companyInfo=redisCompanyInfo.getCacheObject(cname);
            if(companyInfo==null){
                companyInfo=companyInfoDao.findCompanyInfoByName(cname);
                if(companyInfo!=null){
                    redisCompanyInfo.setCacheObject(cname,companyInfo);
                }
            }
            return companyInfo;
        }catch (NullPointerException e){
            return null;
        }
    }
}
